package com.bk.service.log;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bk.entity.EmpSignLog;
import com.bk.mapper.log.EmpLogMapper;
import com.bk.service.RedisService;
import com.bk.util.ReturnUtil;
import com.bk.util.SerilizableObjectUtil;


@Service/**柜员签到日志*/
public class EmpLogService {

	@Autowired
	private EmpLogMapper empLogMapper;
	
	@Autowired
	private RedisService redisService;

	/**签到记录日志*/
	@Transactional
	public void createEmpSignLog(String time, String empName,String employeeId, String roleName, String desc) {
		EmpSignLog emp=new EmpSignLog(employeeId, empName, time, desc, roleName);
		try {
			empLogMapper.createEmpSignLog(emp);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**获取所有的柜员签到日志*/
	public ReturnUtil<EmpSignLog> getAllEmpSignInLog() {
		List<EmpSignLog> allLog=new ArrayList<>();
		String value=redisService.getValue("getAllEmpSignInLog");
		if(value!=null) {
			allLog = (List<EmpSignLog>) SerilizableObjectUtil.unserizlize(value);
			return new ReturnUtil<>(200, "日志查询成功",allLog);
		}else {
			allLog=empLogMapper.getAllEmpSignInLog();
			redisService.setKey("getAllEmpSignInLog",SerilizableObjectUtil.serialize(allLog));
			return allLog.size()>0 ? new ReturnUtil<>(200, "日志查询成功",allLog):new ReturnUtil<>(400, "没有签到日志信息",null);
		}
		
	}

	/**通过时间进行排序查询*/
	public ReturnUtil<EmpSignLog> queryBySignTimeDescOrAsc(String sort) {
		List<EmpSignLog> allLog=allLog=empLogMapper.queryBySignTimeDescOrAsc(sort);
		return allLog.size()>0 ? new ReturnUtil<>(200, "排序查询成功",allLog):new ReturnUtil<>(400, "查询失败",null);
	}

	/**通过时间来查询签到日志*/
	public ReturnUtil<EmpSignLog> getAllEmpSignInLogByDate(String dateTimeArr) {
		String[] arr = dateTimeArr.split(",");
		if(arr.length>0) {
			List<EmpSignLog> allLog=empLogMapper.getAllEmpSignInLogByDate(arr[0],arr[1]);
			return allLog.size()>0 ? new ReturnUtil<>(200, "范围日期成功",allLog):new ReturnUtil<>(400, "范围日期查询失败",null);
		}else {
			return new ReturnUtil<>(400, "范围日期查询失败",null);
		}
	} 
	
}
